//
//  LGHisReportContentCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/9/29.
//  his报告-content类型

#import "LGHisReportContentCell.h"

@interface LGHisReportContentCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)NSMutableArray *labelArray;

@end

@implementation LGHisReportContentCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        [self.contentView addSubview:self.baseView];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(-viewPix(10));
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(viewPix(10));
            make.height.equalTo(@(0));
        }];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    for (UILabel *label in self.labelArray) {
        [label removeFromSuperview];
    }
    [self.labelArray removeAllObjects];
    
    NSArray *itemArray = dataDic[@"contents"];
    CGFloat top = viewPix(15);
    CGFloat left = viewPix(13);
    CGFloat itemW = (Screen_W-viewPix(58))/2.0;
    CGFloat itemH = viewPix(22);
    CGFloat bottom = top;
    BOOL textArea = YES;
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSDictionary *tempDic = itemArray[i];
        if (LGNSString(tempDic[@"content"]).length == 0) continue;
        NSString *type = LGNSString(tempDic[@"type"]);
        if ([type isEqualToString:@"textarea"]) {
            top = (i == 0)?viewPix(5):bottom;
            left = viewPix(13);
            NSString *content = [NSString stringWithFormat:@"%@\n%@",tempDic[@"keyname"],tempDic[@"content"]];
            CGFloat contentH = [content textHeightWithFont:LGFont(15) maxWidth:Screen_W-viewPix(58) lineSpacing:3];
            UILabel *label = [UILabel lableWithText:content colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:0];
            label.frame = CGRectMake(left, top+viewPix(10), Screen_W-viewPix(58), contentH+viewPix(10));
            label.attributedText = [content attributeString:@{NSFontAttributeName:LGFontWeight(15, UIFontWeightMedium)} range:NSMakeRange(0, LGNSString(tempDic[@"keyname"]).length)];
            [self.baseView addSubview:label];
            [label lineSpacing2:3];
            top = kMaxY(label.frame);
            [self.labelArray addObject:label];
            bottom = kMaxY(label.frame);
            textArea = YES;
        }else{
            NSString *content = [NSString stringWithFormat:@"%@：%@",tempDic[@"keyname"],tempDic[@"content"]];
            top = (left<=itemW+viewPix(5))?top:top+itemH;
            left = (left<=itemW+viewPix(5) && i>0 && textArea == NO)?viewPix(13)+itemW:viewPix(13);
            UILabel *label = [UILabel lableWithText:content colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:0];
            label.frame = CGRectMake(left, top, itemW, itemH);
            label.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#666666"]} range:NSMakeRange(0, LGNSString(tempDic[@"keyname"]).length+1)];
            [self.baseView addSubview:label];
            [self.labelArray addObject:label];
            bottom = kMaxY(label.frame);
            textArea = NO;
        }
    }
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(bottom+viewPix(10)));
    }];
    
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(8), Screen_W-viewPix(32), viewPix(150));
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(NSMutableArray *)labelArray{
    if(!_labelArray){
        _labelArray = [NSMutableArray array];
    }
    return _labelArray;
}

@end
